#coding:utf-8
# 运行主程序
import sys
sys.path.append("F:/A_RuanJian/Py_project/imooc/")
from data.runmethod import RunMethod
from data.get_data import GetData
from util.common_util import CommonUtil
from data.dependent_data import DependdentData
# from util.send_email import SendEmail
from util.operation_cookie import OperationCookie
from util.operation_json import OperetionJson,Path
import requests;import urllib3;urllib3.disable_warnings();import json

# headers = {
#     'Accept-Encoding': "gzip, deflate",
#     'Content-Type': "application/x-www-form-urlencoded",
#     'Host': "api.zxyjsc.com",
#     'Connection': "Keep-Alive",
#     'User-Agent': "okhttp/3.8.0",
#     'Accept': "*/*",
#     'cache-control': "no-cache"
#     }

class RunTest:
	def __init__(self):
		self.cookie_json_path=Path.path_cookie
		self.run_method = RunMethod()
		self.data = GetData()
		self.com_util = CommonUtil()
		# self.send_mai = SendEmail()

	#程序执行的
	def go_on_run(self):
		res = None

		pass_count = []
		fail_count = []
		leavel = []
		#10  0,1,2,3
		rows_count = self.data.get_case_lines()
		for i in range(1,rows_count):
			# 判断是否执行
			is_run = self.data.get_is_run(i)
			if is_run:
				url = self.data.get_request_url(i)
				method = self.data.get_request_method(i)
				request_data = self.data.get_data_for_json(i)
				header = self.data.get_header_for_json(i)
				header_type = self.data.get_header_type(i)
				expect = self.data.get_expcet_data(i)
				cookie = self.data.is_cookie(i)
				depend_case = self.data.is_depend(i)

				if depend_case != None:
					self.depend_data = DependdentData(depend_case)
					# 获取的依赖响应数据
					depend_response_data = self.depend_data.get_data_for_key(i)
					# 获取依赖的key: 获取将key分隔，并经过转格式后的list；然后获取list的长度
					int_list = self.data.get_depend_field(i);len01 = len(int_list)
					# request_data[depend_key] = depend_response_data
					# 把处理后的key数据字典的索引中，目前支持的索引最大深度为3
					if len01 == 1:
						b01 = int_list[0]
						request_data[b01] = depend_response_data
					elif len01 == 2:
						b01 = int_list[0];b02 = int_list[1]
						request_data[b01][b02] = depend_response_data
					elif len01 == 3:
						b01 = int_list[0];b02 = int_list[1];b03 = int_list[2]
						request_data[b01][b02][b03] = depend_response_data


				# 判断header是否为application/json
				if header_type == 'not_json':
					request_data = json.dumps(request_data)
				else:
					pass

				# 写死了cookie == 'write'的请求方式，为了得到源res从而获得cookie
				if cookie == 'write':
					res_source = requests.request(method,url,data=request_data,verify=False, headers=header)
					res = json.dumps(res_source.json(), ensure_ascii=False)
					op_cookie = OperationCookie(res_source)
					op_cookie.write_cookie()

				elif cookie == 'yes':
					op_json = OperetionJson(Path.path_cookie)
					cookie= op_json.get_data('__outh')
					cookies = {
						'__outh':cookie
					}

					res = self.run_method.run_main(method,url,request_data,cookies,header)
				else:
					res = self.run_method.run_main(method,url,request_data,header)
				print res

			

				if self.com_util.is_equal_dict(expect,res) == 0:
					self.data.write_result(i,'pass')
					pass_count.append(i)
				else:
					self.data.write_result(i,res)
					fail_count.append(i)


		# self.send_mai.send_main(pass_count,fail_count)


if __name__ == '__main__':
	run = RunTest()
	run.go_on_run()

